package org.dync.giftlibrary.util;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorGroup;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.components.element.FrameAnimationElement;

/**
 * Created by KathLine on 2017/1/8.
 */
public class GiftAnimationUtil {
    /**
     * 礼物飞入动画
     *
     * @param target
     * @param star 动画起始坐标
     * @param end 动画终止坐标
     * @param duration 持续时间
     * @param interpolator
     * @return 创建一个从左到右的飞入动画
     */
    public static AnimatorProperty createFlyFromLtoR(final Component target, float star, float end, int duration, Animator.TimelineCurve interpolator) {
        //1.个人信息先飞出来
        AnimatorProperty anim1 = new AnimatorProperty();
        anim1.setTarget(target);
        anim1.moveFromX(star);
        anim1.moveToX(end);
        if (interpolator != null) anim1.setCurve(interpolator);
        anim1.setDuration(duration);
        return anim1;
    }

    /**
     * startAnimationDrawable
     *
     * @param target
     * @return 播放帧动画
     */
    public static FrameAnimationElement startAnimationDrawable(Image target) {
        FrameAnimationElement animationDrawable = new FrameAnimationElement();
        target.setImageElement(animationDrawable);
        if (animationDrawable != null) {
            target.setVisibility(Component.VISIBLE);
            animationDrawable.start();
        }
        return animationDrawable;
    }


    /**
     * setAnimationDrawable
     *
     * @param target
     * @param drawable 设置帧动画
     */
    public static void setAnimationDrawable(Image target, FrameAnimationElement drawable) {
        target.setImageElement(drawable);
    }

    /**
     * scaleGiftNum
     *
     * @param target
     * @return 送礼数字变化
     */
    public static AnimatorValue scaleGiftNum(final Text target) {
        AnimatorValue anim = new AnimatorValue();
        anim.setDuration(400);
        anim.setValueUpdateListener((animatorValue, value) -> {
            float sc, al;
            if (value < 1 / 2.0) {
                sc = 1.2f - 0.4f * value * 2;
                al = 1.0f - value * 2;
            } else {
                sc = 0.8f + 0.2f * (value - 0.5f) * 2;
                al = (value - 0.5f) * 2;
            }
            target.setScale(sc, sc);
            target.setAlpha(al);
        });

        return anim;

    }

    /**
     * createFadeAnimator
     *
     * @param target
     * @param star
     * @param end
     * @param duration
     * @param startDelay
     * @return 向上飞 淡出
     */
    public static AnimatorProperty createFadeAnimator(final Component target, float star, float end, int duration, int startDelay) {
        AnimatorProperty animator = new AnimatorProperty();
        animator.setTarget(target);
        animator.moveFromY(star);
        animator.moveToY(end);
        animator.alphaFrom(1.0f);
        animator.alpha(0f);
        animator.setDuration(duration);
        animator.setDelay(startDelay);
        return animator;
    }

    /**
     * startAnimation
     *
     * @param animator1
     * @param animator2
     * @return 按顺序播放动画
     */
    public static AnimatorGroup startAnimation(AnimatorProperty animator1, AnimatorProperty animator2) {
        AnimatorGroup animSet = new AnimatorGroup();
        animSet.runSerially(animator1, animator2);
        return animSet;
    }
}
